package com.wanmait.spacesys.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.wanmait.spacesys.dao.CommentReplyDAO;
import com.wanmait.spacesys.vo.Comment;
import com.wanmait.spacesys.vo.Comment2;
import com.wanmait.spacesys.vo.Post;
import com.wanmait.spacesys.vo.Userinfo;

/**
 * 回复评论:控制层
 */
@WebServlet("/CommentReplyController")
public class CommentReplyController extends BaseController {
	//通过用户id获得用户发布所有的帖子
	protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Userinfo userinfo=(Userinfo)request.getSession().getAttribute("userinfo");
		String pageNumStr=request.getParameter("pageNum");	
		
		int pageNum=1;
		try {
			pageNum=Integer.parseInt(pageNumStr);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			/* e.printStackTrace(); */
		}
		if(userinfo!=null) {
			List<Post> posts=CommentReplyDAO.getCommentReplyDAO().findAllById(userinfo.getId(),pageNum);
			int count=CommentReplyDAO.getCommentReplyDAO().getCount(userinfo.getId());
			/* System.out.println(count); */
			request.setAttribute("posts", posts);
			request.setAttribute("count", count);
			this.forward("/web/user/reply.jsp", request, response);
		}
	}
	//通过用户id和帖子id查询帖子详细内容
	protected void myfindAllByPostId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Userinfo userinfo=(Userinfo)request.getSession().getAttribute("userinfo");
		String ids=request.getParameter("id");
		Integer id=Integer.parseInt(ids);
		if(userinfo!=null) {
			 Post post=CommentReplyDAO.getCommentReplyDAO().myfindAllByPostId(userinfo.getId(), id);
			 
			List<Comment> comments=CommentReplyDAO.getCommentReplyDAO().myFindAllPost(id);
			request.setAttribute("comments", comments);
			
			 request.setAttribute("post", post);
			 this.forward("/web/user/commentDisplay.jsp", request, response);
		}
	}
	//通过用户id和评论帖子的上一级id获得帖子的回复内容
	protected void replyToPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	     Userinfo userinfo=(Userinfo)request.getSession().getAttribute("userinfo");
	     String pids=request.getParameter("pid");
	     String name=request.getParameter("name");
	     Integer pid=Integer.parseInt(pids);
	     if(userinfo!=null) {

	    	 List<Comment2> allComment=CommentReplyDAO.getCommentReplyDAO().replyToPost(pid, userinfo.getId());
	         request.setAttribute("allComment", allComment);
			/* request.setAttribute("name", name); */

	         this.forward("/web/user/aad.jsp", request, response);

	        
	     }
	}
	@Override
	protected void doDefault(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.findAll(request, response);
		
	}

}
